/*
 * @Author: Lin Hongwei
 * @Date: 2022-09-08 21:52:49
 * @LastEditTime: 2022-09-16 11:23:49
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 公用函数
 */

//判断是否为空
export const isFalse = (value: unknown) => (value === 0 ? false : !value);

//没有意义的值，可以避免在判断删除时出现false这种被误删
export const isVoid = (value: unknown) =>
  value === undefined || value === null || value === "";

/**
 * @description: 清除对象中的空值
 * @param {*} obj
 * object 类型的话太广了，会出现空对象引用错误
 * 改为键值对的方式
 */
export const cleanObject = (obj: { [keyStr: string]: unknown }) => {
  //函数改变参数本身是不好的（尽量为纯函数）
  const result = { ...obj };
  Object.keys(obj).forEach((key) => {
    // ----@ts-ignore
    // 先暂时隐藏错误
    const value = obj[key];
    if (isVoid(value)) {
      // ----@ts-ignore
      // 先暂时隐藏错误
      delete result[key];
    }
  });
  return result;
};

/**
 * @description: 重置路由到根路由
 */
//  export const resetRouter = ()=>location.href = location.host+ location.port;
export const resetRouter = () =>
  (window.location.href = window.location.origin);
